<?php
/* ******************************
	Incoming Tasks aJax actions
   ******************************
*/
	include('../../includes/classes/core.php');
	$action = $_REQUEST['act'];
	$start		=	$_REQUEST['start'];
    $end		=	$_REQUEST['end'];

	switch ($action) {
	    case 'get_list':
    	    $count = $_REQUEST['count'];
    	    
    	    
/*     	    SELECT 	        warehouse.id,
    	    IFNULL(CONCAT('ა – ',	a_letter.id),CONCAT('დ – ',	d_letter.id)),
    	    CONCAT(client.`name`,'/',client_objects.`name`),
    	    COUNT(*),
    	    IFNULL(a_letter.fact_end_date,d_letter.fact_end_date)as მუშ_დას_დრო,
    	    IFNULL(a_letter.planned_end_date,d_letter.planned_end_date)as მუშ_გეგმ_დრო,
    	    IFNULL((if(TIMEDIFF(a_letter.planned_end_date,a_letter.fact_end_date)<0,CONCAT('<span style=\"background-color: red\">',TIMEDIFF(a_letter.planned_end_date,a_letter.fact_end_date),'</span>'),TIMEDIFF(a_letter.planned_end_date,a_letter.fact_end_date))),
    	    (if(TIMEDIFF(d_letter.planned_end_date,d_letter.fact_end_date)<0,CONCAT('<span style=\"background-color: red\">',TIMEDIFF(d_letter.planned_end_date,d_letter.fact_end_date),'</span>'),TIMEDIFF(d_letter.planned_end_date,d_letter.fact_end_date)))) as სამუშაოს_სხვაობა,
    	    IFNULL(a_letter.end_date,d_letter.end_date),
    	    IFNULL(a_letter.finish_date,d_letter.finish_date),
    	    IFNULL((if(TIMEDIFF(a_letter.end_date,a_letter.finish_date)<0,CONCAT('<span style=\"background-color: red\">',TIMEDIFF(a_letter.end_date,a_letter.finish_date),'</span>'),TIMEDIFF(a_letter.end_date,a_letter.finish_date))),
    	    (if(TIMEDIFF(d_letter.end_date,d_letter.finish_date)<0,CONCAT('<span style=\"background-color: red\">',TIMEDIFF(d_letter.end_date,d_letter.finish_date),'</span>'),TIMEDIFF(d_letter.end_date,d_letter.finish_date)))) as სამუშაოს_სხვაობა
    	    FROM warehouse
    	    LEFT JOIN a_letter ON a_letter.id=warehouse.a_letter_id
    	    LEFT JOIN d_letter ON d_letter.id=warehouse.d_letter_id
    	    LEFT JOIN client_objects ON client_objects.id = warehouse.client_object_id
    	    LEFT JOIN client ON client.id=client_objects.client_id
    	    LEFT JOIN	warehouse_detail ON warehouse_detail.warehouse_id=warehouse.id AND warehouse_detail.actived=1
    	    WHERE    (IFNULL(d_letter.end_date,a_letter.end_date)) >= '$start' and (IFNULL(d_letter.end_date,a_letter.end_date)) <= '$end'
    	        GROUP BY warehouse.id */
    	    
    	    $rResult = mysql_query("
SELECT id,
       letter,
       object,
       count,
       fact_end_date,
	   planned_end_date,
       IF(TIMEDIFF(planned_end_date,fact_end_date)>0,
	 		TIMEDIFF(planned_end_date,fact_end_date),
	 		CONCAT('<span style=\"background-color: red\">',TIMEDIFF(planned_end_date,fact_end_date),'</span>')),
	   end_date,
       IF(finish_date1>0, finish_date1, finish_date),       
       IF(TIMEDIFF(end_date,IF(finish_date1>0, finish_date1, finish_date))>0,
	 		TIMEDIFF(end_date,IF(finish_date1>0, finish_date1, finish_date)),
	 	    CONCAT('<span style=\"background-color: red\">',TIMEDIFF(end_date,IF(finish_date1>0, finish_date1, finish_date)),'</span>'))
FROM (SELECT 	warehouse.id,
IFNULL(CONCAT('ა – ',	a_letter.id),CONCAT('დ – ',	d_letter.id)) AS letter,
CONCAT(client.`name`,'/',client_objects.`name`) AS object,
COUNT(*) as count,
IFNULL(a_letter.fact_end_date,d_letter.fact_end_date)as fact_end_date,
IFNULL(a_letter.planned_end_date,d_letter.planned_end_date)as planned_end_date,
IFNULL(a_letter.end_date,d_letter.end_date) AS end_date,
IFNULL(rs_wdsl_status.end_date,0) as finish_date1,
COALESCE(a_letter.finish_date,d_letter.finish_date,0) AS finish_date-- ,
FROM warehouse
LEFT JOIN a_letter ON a_letter.id=warehouse.a_letter_id
LEFT JOIN d_letter ON d_letter.id=warehouse.d_letter_id
LEFT JOIN client_objects ON client_objects.id = warehouse.client_object_id
LEFT JOIN client ON client.id=client_objects.client_id
LEFT JOIN rs_wdsl_status ON rs_wdsl_status.id= IFNULL(b_letter_overhead_id,e_letter_overhead_id)
JOIN	warehouse_detail ON warehouse_detail.warehouse_id=warehouse.id     AND warehouse_detail.actived=1
WHERE    (IFNULL(d_letter.end_date,a_letter.end_date)) >= '$start' AND (IFNULL(d_letter.end_date,a_letter.end_date)) <= '$end'
GROUP BY warehouse.id) AS my");

			$output = array(
				"aaData"	=> array()
			);

			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					/* General output */
					$row[] = $aRow[$i];
				}
				$output['aaData'][] = $row;
			}

			echo json_encode( $output );

	        break;
	    default:
	       	echo "null";
	}
?>